package com.jams.goodox.activity.mine.question;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.jams.goodox.BaseActivity;
import com.jams.goodox.R;
import com.jams.goodox.net.BaseEntity;
import com.jams.goodox.net.ProtocolManagers;
import com.jams.goodox.net.RetrofitCallbackListener;
import com.jams.goodox.net.services.MineService;
import com.jams.goodox.utils.StatusBarCompat;
import com.jams.goodox.utils.StyleUtil;
import com.jams.goodox.utils.Utils;
import com.jams.library.commonAdapter.RecycleCommonAdapter;
import com.jams.library.commonAdapter.RecycleCommonViewHolder;
import com.jams.library.listener.OnRecycleItemClickListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import retrofit2.Call;

/**
 * Created by Administrator on 2018/5/12.
 */

public class MineQuestionActivity extends BaseActivity {
    @Bind(R.id.do_answer)
    RecyclerView doAnswer;
    @Bind(R.id.linear_do_answer)
    LinearLayout linearDoAnswer;
    @Bind(R.id.un_answer)
    RecyclerView unAnswer;
    @Bind(R.id.linear_un_answer)
    LinearLayout linearUnAnswer;

    private List<MineQuestionBean> doMineQuestionBeanList;
    private List<MineQuestionBean> unMineQuestionBeanList;

    @Override
    protected int getLayoutId() {
        return R.layout.mine_question_activity;
    }

    @Override
    protected void init() {
        StyleUtil.titleBackKey(this, "我的问答");
        StatusBarCompat.compat(this, ContextCompat.getColor(this, R.color.main_theme));//沉侵式头部

        doMineQuestionBeanList=new ArrayList<>();
        doAnswer.setLayoutManager(new LinearLayoutManager(this));
        doAnswer.setAdapter(new RecycleCommonAdapter<MineQuestionBean>(this,doMineQuestionBeanList,R.layout.mine_question_item) {

            @Override
            public void convert(RecycleCommonViewHolder helper,final MineQuestionBean item, int position) {
                helper.setText(R.id.title,item.getTitle());
                helper.setText(R.id.nickName,item.getNickName());
                helper.setText(R.id.individualitySign,item.getIndividualitySign());
                helper.setText(R.id.time, Utils.getFormatDateStr(item.getCreateTime()));
                helper.setSimpleDraweeView(R.id.sdv_identify,item.getImgPath());
                helper.setOnItemClickListener(new OnRecycleItemClickListener() {
                    @Override
                    public void itemClick() {
                        Bundle bundle=new Bundle();
                        bundle.putInt("questionId",item.getId());
                        qStartActivity(MineQuestionDetailActivity.class,bundle);
                    }
                });
            }
        });


        unMineQuestionBeanList=new ArrayList<>();
        unAnswer.setLayoutManager(new LinearLayoutManager(this));
        unAnswer.setAdapter(new RecycleCommonAdapter<MineQuestionBean>(this,unMineQuestionBeanList,R.layout.mine_question_item) {

            @Override
            public void convert(RecycleCommonViewHolder helper, final MineQuestionBean item, int position) {
                helper.setText(R.id.title,item.getTitle());
                helper.setText(R.id.nickName,item.getNickName());
                helper.setText(R.id.individualitySign,item.getIndividualitySign());
                helper.setText(R.id.time, Utils.formatDateTime(item.getCreateTime()));
                helper.setSimpleDraweeView(R.id.sdv_identify,item.getImgPath());
                helper.setOnItemClickListener(new OnRecycleItemClickListener() {
                    @Override
                    public void itemClick() {
                        Bundle bundle=new Bundle();
                        bundle.putInt("questionId",item.getId());
                        qStartActivity(MineQuestionDetailActivity.class,bundle);
                    }
                });
            }
        });

        getData();
    }

    private void getData(){
        enqueue(createCallService(MineService.class).queueQuestion(ProtocolManagers.getDefaultMap()), new RetrofitCallbackListener<BaseEntity>() {
            @Override
            public void onSuccess(Call<BaseEntity> call, BaseEntity result) {
                if (TextUtils.isEmpty(result.getResponseData())) return;
                try{
                    JsonArray jsonArray = new JsonParser().parse(result.getResponseData()).getAsJsonArray();
                    List<MineQuestionBean> mineQuestionBeanList=new ArrayList<>();
                    for(JsonElement jsonElement:jsonArray){
                        MineQuestionBean message = new Gson().fromJson(jsonElement, MineQuestionBean.class);
                        mineQuestionBeanList.add(message);
                    }
                    if(mineQuestionBeanList.size()>0)splitList(mineQuestionBeanList);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(String msg) {

            }
        });
    }

    private void splitList( List<MineQuestionBean> mineQuestionBeanList){
        unMineQuestionBeanList.clear();
        doMineQuestionBeanList.clear();
        for(MineQuestionBean mineQuestionBean:mineQuestionBeanList){
            if(mineQuestionBean.getQueState()==0){
                unMineQuestionBeanList.add(mineQuestionBean);
            }else {
                doMineQuestionBeanList.add(mineQuestionBean);
            }
        }
        if(unMineQuestionBeanList.size()==0){
            linearUnAnswer.setVisibility(View.GONE);
        }else {
            linearUnAnswer.setVisibility(View.VISIBLE);
            unAnswer.getAdapter().notifyDataSetChanged();
        }
        if(doMineQuestionBeanList.size()==0){
            linearDoAnswer.setVisibility(View.GONE);
        }else {
            linearDoAnswer.setVisibility(View.VISIBLE);
            doAnswer.getAdapter().notifyDataSetChanged();
        }
    }
}
